
# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.orderLogDao import OrderLogAPI
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)



# ------------------ order-log 表API ---------------
@app.route('/addOrderLog', methods=['POST'])
def addOrderLog():
    if not request.json:
        abort(400)
    orderLog_info = {
        'orderId': request.json.get('orderId'),
        'userId': request.json.get('userId'),
        'orderActionCode': request.json.get('orderActionCode' ),
        'orderActionDate': request.json.get('orderActionDate'),
        'orderRemark': request.json.get('orderRemark')
    }
    try:
        rs = OrderLogAPI().addOrderLog(orderLog_info)
        if rs:
            return jsonify({  "message": {"code": 1, "info": "添加成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "添加失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/updateOrderLog', methods=['POST'])
def updateOrderLog():
    try:
        id = request.json.get('id')
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = OrderLogAPI().selectOrderLog(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    updateOrderLog = {
        'orderId': request.json.get('orderId', oldInfo[1]),
        'userId': request.json.get('userId', oldInfo[2]),
        'orderActionCode': request.json.get('orderActionCode',oldInfo[3] ),
        'orderActionDate': request.json.get('orderActionDate', oldInfo[4]),
        'orderRemark': request.json.get('orderRemark',oldInfo[5] )
    }
    if not updateOrderLog:
        return jsonify({"message": {"code": 0, "info": "信息传递不完整"}})
    try:
        updateOrderLogResult = OrderLogAPI().updateOrderLogById(updateOrderLog, id)
        if updateOrderLogResult:
            return jsonify({"info": updateOrderLog,
                            "massage": {"code": 1, "info": "订单历史表修改成功"}})
        else:
            return jsonify({"message": {"code": 0, "info": "订单历史表修改失败"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryOrderLogById', methods=['POST'])    #根据order_id查询
def queryOrderLogById():
    orderId = request.json.get('id')
    try:
        queryResult, OrderLogListJson = OrderLogAPI().queryOrderLog(orderId)
        if queryResult:
            return jsonify({"info": OrderLogListJson, "totalCount": queryResult,
                            "message": {"code": 1, "info": "消息查询成功！"}})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryOrderLogByCondition', methods=['POST'])
def queryOrderLogByCondition():
    if not request.json:
        abort(400)
    try:
        pageIndex = request.json["pageIndex"]
        pageSize = request.json["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    userId = request.json.get("userId",1)
    try:
        total_count, UrlManageJSON = OrderLogAPI().queryPointItemsByCondition(pageIndex=pageIndex,pageSize=pageSize,userId=userId)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryOrderLog OK"},"total":total_count,"info":UrlManageJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/deleteOrderLog', methods=['POST'])
def deleteOrderLog():
    orderLogId = request.json.get('ids')
    try:
        deleteOrderLogResult = OrderLogAPI().deleteOrderLogById(orderLogId)
        if deleteOrderLogResult:
            return jsonify({"message": {"code": 1, "info": "删除订单历史记录成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "删除订单历史记录失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})
